<template>
  <div>
    <div
      class="chact_box"
      ref="chactbox"
      v-if="props.msglist.length > 0 "
    >
      <div
        v-for="(item, index) in props.msglist"
        :key="index"
      >
        <div
          class="head-chat"
          :class="['chat-item', item.user_id == props.userinfo ? 'right' : 'left']"
        >
          <div
            class="user"
            style="
              display: flex;
              align-items: center;
              justify-content: space-between;
            "
          >
            <img
              class="avatar"
              :src="item.head_img"
              style="margin-left: 10px"
            >
          </div>
          <div class="meta">
            <p
              class="hd"
              :class="item.user_id == props.userinfo ? 'flex-r' : 'flex-l'"
            >
              <span class="time">{{ item.create_time }}</span>
              <span class="name">{{ item.username }}</span>
            </p>
          </div>
          <div
            style="top: 10px"
            class="content text"
          >
            <span>{{ item.msg }}</span>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, watch, nextTick, onMounted, onUpdated } from 'vue'
const chactbox:any = ref(null)
const props:any = defineProps({
  msglist: {
    type: Object,
    required: true
  },
  userinfo: {
    type: Number,
    required: true
  }
})

watch(() => props.msgList, (newVal, oldVal) => {
  nextTick(() => {
    console.log(123123123)
    chactbox.value.scrollTop = chactbox.value.scrollHeight
  })
},
// 深度监听
{ deep: true, immediate: true }
)

onMounted(() => {
  nextTick(() => {
    chactbox.value.scrollTop = chactbox.value.scrollHeight
    console.log(chactbox.value.scrollTop, chactbox, 9999)
  })
})

onUpdated(() => {
  nextTick(() => {
    chactbox.value.scrollTop = chactbox.value.scrollHeight
    console.log(chactbox.value.scrollTop, chactbox, 9999)
  })
})

</script>

<style lang="less" scope>
/* 聊天 */
.chat {
  // height:500px;
  overflow: auto;
  margin: 37px 0;
  // background: rgba(247, 249, 252, 1);
  // padding: 30px;
  position: relative;
  .addChat {
    width: 90%;
    position: absolute;
    bottom: 33px;
    left: 50%;
    transform: translateX(-50%);
  }
}
.v_btn {
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  z-index: 11;
  .iconSvg {
    width: 50px;
    height: 50px;
  }
}
.chact_box {
  padding-bottom: 60px;
  overflow: auto;
  height: calc(100vh - 360px);
}
.chat-item {
  position: relative;
  padding: 10px 58px;
  min-height: 60px;
  box-sizing: border-box;
  overflow: hidden;
  margin-bottom: 10px;
  .user {
    position: absolute;
    // top: 50%;
    // transform: translateY(-50%);
    .avatar {
      display: block;
      width: 40px;
      height: 40px;
      border-radius: 50px;
    }
  }

  .meta {
    font-size: 14px;
    // padding: 0 0 10px;
    // display: flex;
    align-items: flex-end;
    flex-wrap: wrap;

    .hd {
      margin: 0;
      padding: 0;
    }

    .name {
      // font-weight: 600;
      font-size: 13px;
      vertical-align: bottom;
      color: rgb(80, 139, 255);
    }

    .time {
      padding: 0 10px;
      margin: 5px 0;
      font-size: 12px;
      color: rgb(80, 139, 255);
    }

    .level-icon {
      width: 20px;
      vertical-align: middle;
      display: inline-block;
      margin-right: 7px;
    }
  }

  .content {
    text-align: left;
    position: relative;
    display: inline-block;
    max-width: 400px;
    min-width: 80px;
    min-height: 26px;
    box-sizing: border-box;
    font-size: 14px;
    line-height: 26px;
    padding: 7px;
    color: #9a9a9a;
    background-color: #fff;
    word-break: break-all;
    border-radius: 5px;
    // border: 1px solid #d6d6d6;
    // &::after {
    //   content: "";
    //   position: absolute;
    //   top: 15px;
    //   width: 0;
    //   height: 0;
    //   border-style: solid dashed dashed;
    //   border-color: #fff transparent transparent;
    //   overflow: hidden;
    //   border-width: 10px;
    // }

    &.text {
      white-space: pre-line;
      user-select: all;
    }

    &.img,
    &.video {
      background: none;
      padding: 0;
      overflow: hidden;

      &::after {
        display: none;
      }

      img {
        display: block;
        max-height: 120px;
        max-width: 100%;
      }
    }
  }
  &.left {
    text-align: left;

    .user {
      left: 8px;
    }

    .meta {
      flex-direction: row;

      .label {
        margin-right: 5px;
      }
    }

    .content {
      &::after {
        left: -10px;
      }
    }
  }

  &.right {
    text-align: right;

    .user {
      right: 8px;
    }

    .meta {
      flex-direction: row-reverse;

      .label {
        margin-left: 5px;
      }
    }

    .content {
      &::after {
        right: -10px;
      }
    }
  }
}
.right .content {
  left: -12px;
  top: 10px;
}
.left .name {
  // margin-left: 17px;
}
.left .content {
  left: 12px;
  top: 10px;
}

@media (min-width: 750px) {
  /* 聊天 */
  .chat {
    // height:500px;
    overflow: auto;
    margin: 37px 0;
    // background: rgba(247, 249, 252, 1);
    // padding: 30px;
    position: relative;
    .addChat {
      width: 90%;
      position: absolute;
      bottom: 33px;
      left: 50%;
      transform: translateX(-50%);
    }
  }
  .v_btn {
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    z-index: 11;
    .iconSvg {
      width: 50px;
      height: 50px;
    }
  }
  .chact_box {
    padding-bottom: 60px;
    overflow: auto;
  }
  .chat-item {
    position: relative;
    padding: 10px 58px;
    min-height: 60px;
    box-sizing: border-box;
    overflow: hidden;
    margin-bottom: 10px;
    .user {
      position: absolute;
      // top: 50%;
      // transform: translateY(-50%);
      .avatar {
        display: block;
        width: 40px;
        height: 40px;
        border-radius: 50px;
      }
    }

    .meta {
      font-size: 14px;
      // padding: 0 0 10px;
      // display: flex;
      align-items: flex-end;
      flex-wrap: wrap;

      .hd {
        margin: 0;
        padding: 0;
      }

      .name {
        // font-weight: 600;
        font-size: 13px;
        vertical-align: bottom;
        color: rgb(80, 139, 255);
      }

      .time {
        padding: 0 10px;
        margin: 5px 0;
        font-size: 12px;
        color: rgb(80, 139, 255);
      }

      .level-icon {
        width: 20px;
        vertical-align: middle;
        display: inline-block;
        margin-right: 7px;
      }
    }

    .content {
      text-align: left;
      position: relative;
      display: inline-block;
      max-width: 400px;
      min-width: 80px;
      min-height: 26px;
      box-sizing: border-box;
      font-size: 14px;
      line-height: 26px;
      padding: 7px;
      color: #9a9a9a;
      background-color: #fff;
      word-break: break-all;
      border-radius: 5px;
      // border: 1px solid #d6d6d6;
      // &::after {
      //   content: "";
      //   position: absolute;
      //   top: 15px;
      //   width: 0;
      //   height: 0;
      //   border-style: solid dashed dashed;
      //   border-color: #fff transparent transparent;
      //   overflow: hidden;
      //   border-width: 10px;
      // }

      &.text {
        white-space: pre-line;
        user-select: all;
      }

      &.img,
      &.video {
        background: none;
        padding: 0;
        overflow: hidden;

        &::after {
          display: none;
        }

        img {
          display: block;
          max-height: 120px;
          max-width: 100%;
        }
      }
    }
    &.left {
      text-align: left;

      .user {
        left: 8px;
      }

      .meta {
        flex-direction: row;

        .label {
          margin-right: 5px;
        }
      }

      .content {
        &::after {
          left: -10px;
        }
      }
    }

    &.right {
      text-align: right;

      .user {
        right: 8px;
      }

      .meta {
        flex-direction: row-reverse;

        .label {
          margin-left: 5px;
        }
      }

      .content {
        &::after {
          right: -10px;
        }
      }
    }
  }
  .right .content {
    left: -12px;
    top: 10px;
  }
  .left .name {
    // margin-left: 17px;
  }
  .left .content {
    left: 12px;
    top: 10px;
  }
  /* 聊天 */
}
@media (min-width: 750px) {
  /* 聊天 */
.chat {
  // height:500px;
  overflow: auto;
  margin: 37px 0;
  // background: rgba(247, 249, 252, 1);
  // padding: 30px;
  position: relative;
  .addChat {
    width: 90%;
    position: absolute;
    bottom: 33px;
    left: 50%;
    transform: translateX(-50%);
  }
}
.v_btn {
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  z-index: 11;
  .iconSvg {
    width: 50px;
    height: 50px;
  }
}
.chact_box {
  padding-bottom: 60px;
  overflow: auto;
  height: calc(100vh - 360px);
}
.chat-item {
  position: relative;
  padding: 10px 58px;
  min-height: 60px;
  box-sizing: border-box;
  overflow: hidden;
  margin-bottom: 10px;
  .user {
    position: absolute;
    // top: 50%;
    // transform: translateY(-50%);
    .avatar {
      display: block;
      width: 40px;
      height: 40px;
      border-radius: 50px;
    }
  }

  .meta {
    font-size: 14px;
    // padding: 0 0 10px;
    // display: flex;
    align-items: flex-end;
    flex-wrap: wrap;

    .hd {
      margin: 0;
      padding: 0;
    }

    .name {
      // font-weight: 600;
      font-size: 13px;
      vertical-align: bottom;
      color: rgb(80, 139, 255);
    }

    .time {
      padding: 0 10px;
      margin: 5px 0;
      font-size: 12px;
      color: rgb(80, 139, 255);
    }

    .level-icon {
      width: 20px;
      vertical-align: middle;
      display: inline-block;
      margin-right: 7px;
    }
  }

  .content {
    text-align: left;
    position: relative;
    display: inline-block;
    max-width: 400px;
    min-width: 80px;
    min-height: 26px;
    box-sizing: border-box;
    font-size: 14px;
    line-height: 26px;
    padding: 7px;
    color: #9a9a9a;
    background-color: #fff;
    word-break: break-all;
    border-radius: 5px;
    // border: 1px solid #d6d6d6;
    // &::after {
    //   content: "";
    //   position: absolute;
    //   top: 15px;
    //   width: 0;
    //   height: 0;
    //   border-style: solid dashed dashed;
    //   border-color: #fff transparent transparent;
    //   overflow: hidden;
    //   border-width: 10px;
    // }

    &.text {
      white-space: pre-line;
      user-select: all;
    }

    &.img,
    &.video {
      background: none;
      padding: 0;
      overflow: hidden;

      &::after {
        display: none;
      }

      img {
        display: block;
        max-height: 120px;
        max-width: 100%;
      }
    }
  }
  &.left {
    text-align: left;

    .user {
      left: 8px;
    }

    .meta {
      flex-direction: row;

      .label {
        margin-right: 5px;
      }
    }

    .content {
      &::after {
        left: -10px;
      }
    }
  }

  &.right {
    text-align: right;

    .user {
      right: 8px;
    }

    .meta {
      flex-direction: row-reverse;

      .label {
        margin-left: 5px;
      }
    }

    .content {
      &::after {
        right: -10px;
      }
    }
  }
}
.right .content {
  left: -12px;
  top: 10px;
}
.left .name {
  // margin-left: 17px;
}
.left .content {
  left: 12px;
  top: 10px;
}

@media (min-width: 750px) {
  /* 聊天 */
  .chat {
    // height:500px;
    overflow: auto;
    margin: 37px 0;
    // background: rgba(247, 249, 252, 1);
    // padding: 30px;
    position: relative;
    .addChat {
      width: 90%;
      position: absolute;
      bottom: 33px;
      left: 50%;
      transform: translateX(-50%);
    }
  }
  .v_btn {
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    z-index: 11;
    .iconSvg {
      width: 50px;
      height: 50px;
    }
  }
  .chact_box {
    padding-bottom: 60px;
    overflow: auto;
  }
  .chat-item {
    position: relative;
    padding: 10px 58px;
    min-height: 60px;
    box-sizing: border-box;
    overflow: hidden;
    margin-bottom: 10px;
    .user {
      position: absolute;
      // top: 50%;
      // transform: translateY(-50%);
      .avatar {
        display: block;
        width: 40px;
        height: 40px;
        border-radius: 50px;
      }
    }

    .meta {
      font-size: 14px;
      // padding: 0 0 10px;
      // display: flex;
      align-items: flex-end;
      flex-wrap: wrap;

      .hd {
        margin: 0;
        padding: 0;
      }

      .name {
        // font-weight: 600;
        font-size: 13px;
        vertical-align: bottom;
        color: rgb(80, 139, 255);
      }

      .time {
        padding: 0 10px;
        margin: 5px 0;
        font-size: 12px;
        color: rgb(80, 139, 255);
      }

      .level-icon {
        width: 20px;
        vertical-align: middle;
        display: inline-block;
        margin-right: 7px;
      }
    }

    .content {
      text-align: left;
      position: relative;
      display: inline-block;
      max-width: 400px;
      min-width: 80px;
      min-height: 26px;
      box-sizing: border-box;
      font-size: 14px;
      line-height: 26px;
      padding: 7px;
      color: #9a9a9a;
      background-color: #fff;
      word-break: break-all;
      border-radius: 5px;
      // border: 1px solid #d6d6d6;
      // &::after {
      //   content: "";
      //   position: absolute;
      //   top: 15px;
      //   width: 0;
      //   height: 0;
      //   border-style: solid dashed dashed;
      //   border-color: #fff transparent transparent;
      //   overflow: hidden;
      //   border-width: 10px;
      // }

      &.text {
        white-space: pre-line;
        user-select: all;
      }

      &.img,
      &.video {
        background: none;
        padding: 0;
        overflow: hidden;

        &::after {
          display: none;
        }

        img {
          display: block;
          max-height: 120px;
          max-width: 100%;
        }
      }
    }
    &.left {
      text-align: left;

      .user {
        left: 8px;
      }

      .meta {
        flex-direction: row;

        .label {
          margin-right: 5px;
        }
      }

      .content {
        &::after {
          left: -10px;
        }
      }
    }

    &.right {
      text-align: right;

      .user {
        right: 8px;
      }

      .meta {
        flex-direction: row-reverse;

        .label {
          margin-left: 5px;
        }
      }

      .content {
        &::after {
          right: -10px;
        }
      }
    }
  }
  .right .content {
    left: -12px;
    top: 10px;
  }
  .left .name {
    // margin-left: 17px;
  }
  .left .content {
    left: 12px;
    top: 10px;
  }
  /* 聊天 */
}
}
</style>
